package com.pl.service.impl;

import com.alibaba.excel.EasyExcel;
import com.pl.entity.Device;
import com.pl.dao.DeviceDao;
import com.pl.entity.SysUser;
import com.pl.lister.SysUserReadLister;
import com.pl.lister.deviceReadLister;
import com.pl.service.DeviceService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (Device)表服务实现类
 *
 * @author makejava
 * @since 2024-04-30 15:25:02
 */
@Service("deviceService")
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceDao deviceDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Device queryById(Integer id) {
        return this.deviceDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<Device> queryByPage(Map<String, Object> map) {
        return this.deviceDao.queryByPage(map);
    }

    /**
     * 新增数据
     *
     * @param device 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(Device device) {
        device.setAntime(new Date());
        int num = this.deviceDao.insert(device);
        if (num > 0){
            return true;
        }
        return false;
    }

    /**
     * 修改数据
     *
     * @param device 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(Device device) {
       int num  = this.deviceDao.update(device);
       if (num > 0){
           return true;
       }
       return false;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(int id) {
        return this.deviceDao.deleteById(id) > 0;
    }
    //批量删除
    @Override
    public boolean deleteBatch(List<Integer> list) {
        int i = deviceDao.deleteBatch(list);
        if (i > 0) {
            return true;
        }
        return false;
    }
    /**
     * 批量添加，用于文件导入时 配合监听器使用
     *
     * @param devicesList
     * @return
     */
    @Override
    public boolean inserts(List<Device> devicesList) {
        int num = deviceDao.insertBatch(devicesList);
        if (num > 0) {
            return true;
        }
        return false;
    }

//导入
    @Override
    public String importSysUserFile(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), Device.class, new deviceReadLister(this)).sheet("设备信息").doReadSync();
            return "import_success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";
    }
//导出
    @Override
    public void exportDeviceFile(HttpServletResponse response) {
        List<Device> devicesList = deviceDao.finAll();
        try {
            EasyExcel.write(response.getOutputStream(), Device.class).sheet("设备信息").doWrite(devicesList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //查询全部
    @Override
    public boolean finAll() {
        List<Device> users = deviceDao.finAll();
        if (users != null) {
            return true;
        }
        return false;
    }
}
